from random import randint


class ICBC:
    UserDataBase = {
        'admin': {
            'account': '12345678',
            'name': 'admin',
            'passwd': 123456,
            'address': '北京市',
            'balance': 0,
            'bank': "中国工商银行的昌平支行"
        },
        'lisi': {
            'account': '75219863',
            'name': 'lisi',
            'passwd': 123456,
            'address': '北京市',
            'balance': 4051,
            'bank': "中国工商银行的昌平支行"
        }
    }  # 用户数据库

    def __init__(self):
        self.name = '中国工商银行'

    def address_data(self, province, street, doorplate):  # 地址程序
        country = "中国"
        return country + province + street + doorplate

    def userdata(self, name, passwd, address, balance):  # 用户程序 生成账号
        if len(name) == 0 or len(address) == 0:  # 检查名字与地址合法性
            print("名字和地址不能为空！")
            return -1
        if len(str(passwd)) != 6:  # 检查密码合法性
            print("密码必须为6位！")
            return -2

        passwd, balance = int(passwd), int(balance)  # 规整格式
        account = ""
        flag = 1
        while flag:
            account = ""
            for i in range(8):
                account += str(randint(0, 9))  # 生成8位随机数字，不可用户输入
            for user in self.UserDataBase.items():  # 防止生成已有用户账号
                if account != user[1]['account']:
                    flag = 0
                else:
                    flag = 1
        return {
            'account': account,
            'name': name,
            'passwd': passwd,
            'address': address,
            'balance': balance,
            'bank': "中国工商银行的昌平支行"
        }

    def add_user(self, data) -> int:  # 添加用户
        if data['name'] in self.UserDataBase: return 2  # 用户已存在
        if len(self.UserDataBase) >= 100: return 3  # 用户数据库已满
        user = self.userdata(data['name'], data['passwd'], data['address'], data['balance'])  # 生成账号及加入银行信息
        self.UserDataBase[user['name']] = user
        return 1

    def deposit(self, account, amount) -> bool:  # 存钱程序
        for idx, user in enumerate(self.UserDataBase.items()):  # 查找是否有该用户
            if account == user[1]['account']:
                user[1]['balance'] += amount
                return True
        return False

    def correct_password(self, account, passwd) -> bool:
        for user in self.UserDataBase.items():
            if account == user[1]['account']:
                if passwd == user[1]['passwd']:
                    return True
                else:
                    return False
        else:
            return False

    def withdraw(self, account, passwd, amount) -> int:  # 取钱程序
        for user in self.UserDataBase.items():
            if account == user[1]['account']:
                if passwd == user[1]['passwd']:
                    if amount <= user[1]['balance']:
                        user[1]['balance'] -= amount
                        return 0
                    else:
                        return 3
                else:
                    return 2
        return 1

    def transfer(self, account_out, account_in, passwd, amount) -> int:
        flag = self.withdraw(account_out, passwd, amount)
        if flag: return flag
        if not self.deposit(account_in, amount): return 1
        return 0

    def search_user(self, account, passwd) -> None:  # 查找是否有该用户
        flag = 1
        for user in self.UserDataBase.items():
            if account == user[1]['account']:
                if passwd == user[1]['passwd']:
                    flag = 0
                    print("{},密码:{},余额：{}元，用户居住地址：{}，当前账户的开户行：{}".format(user[1]['account'],
                                                                                            user[1]['passwd'],
                                                                                            user[1]['balance'],
                                                                                            user[1]['address'],
                                                                                            user[1]['bank']))
                else:
                    print("密码错误")
        if flag: print("用户不存在")

    def interface(self) -> None:
        print("\n", "*" * 35, "\n*          中国工商银行", " " * 13, "*\n*          账户管理系统", " " * 13,
              "*\n*             V1.0", " " * 16, "*\n", "*" * 35, "\n*1.开户", " " * 28, "*\n*2.存钱",
              " " * 28, "*\n*3.取钱", " " * 28, "*\n*4.转账", " " * 28, "*\n*5.查询", " " * 28, "*\n*6.Bye!",
              " " * 27, "*\n", "*" * 35)
        # 打印界面信息
        selector = eval(input("请输入您想要办理的业务[1~6]："))
        if selector == 1:
            name, passwd, address, balance = input("请输入您的姓名，密码，地址和余额（使用'；'分号隔开）：").split('；')
            passwd, balance = int(passwd), int(balance)
            data = {
                'name': name,
                'passwd': passwd,
                'address': address,
                'balance': balance,
            }
            result = self.add_user(data)
            if result == 1:
                print("开户成功")
            elif result == 2:
                print("用户已存在！")
            else:
                print("用户库已满！")
        elif selector == 2:
            account, amount = input("请输入您的账号和存款金额并用'；'分号隔开：").split('；')
            amount = int(amount)
            result = self.deposit(account, amount)
            if result:
                print("存款成功")
            else:
                print("用户不存在！")
        elif selector == 3:
            account, passwd, amount = input("请输入您的账号、密码和取款金额并用'；'分号隔开：").split('；')
            passwd, amount = int(passwd), int(amount)
            result = self.withdraw(account, passwd, amount)
            if not result:
                print("取款成功！")
            elif result == 1:
                print("用户不存在！")
            elif result == 2:
                print("密码不正确！")
            elif result == 3:
                print("账户余额不足！")
        elif selector == 4:
            account_out, passwd, amount, account_in = input(
                "请输入您的账号、密码、汇款金额及汇入账号并用'；'分号隔开：").split('；')
            passwd, amount = int(passwd), int(amount)
            result = self.transfer(account_out, account_in, passwd, amount)
            if not result:
                print("转账成功！")
            elif result == 1:
                print("用户不存在！")
            elif result == 2:
                print("密码不正确！")
            elif result == 3:
                print("账号余额不足！")
        elif selector == 5:
            account, passwd = input("请输入您的账号和密码并用'；'分号隔开：").split('；')
            passwd = int(passwd)
            self.search_user(account, passwd)
        elif selector == 6:
            print("Bye Bye~")
        else:
            print("请输入正确的编号[1~6]")
